﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LogisticDatabase.DataAccess;
using LogisticDatabase.Models;
using LogisticDatabase;
using LogisticDatabase.Data;

namespace LogisticDatabase.DataAccess
{
    public class LogisticDataAccess
    {

        public List<Circulation> getCirculations()
        {
            return null;
        }
        public List<Productinstance> getProductinstances()
        {
            return null;
        }
        public List<Factory> getFactorys()
        {
            return null;
        }
        public List<Component> getComponents()
        {
            return null;
        }
        public List<Material> getMaterials()
        {
            return null;
        }
        public List<City> getCitites()
        {

            List<City> cities = new List<City>();
            using (LogisticDatabaseEntity ctx = new LogisticDatabaseEntity())
            {
                cities = ctx.tblCity.Select(t => new City { Id = t.CityID, Name = t.CityName, Country = t.CityCountry }).ToList();
            }
            return cities;
        }
        public List<Factory> getFactoryById()
        {
            return null;
        }

        public void addCity(City city)
        {
            using (LogisticDatabaseEntity ctx = new LogisticDatabaseEntity())
            {
                tblCity tc = new tblCity();
                
                    tc.CityName = city.Name;
                    tc.CityCountry = city.Country;

                    ctx.tblCity.Add(tc);

                    ctx.SaveChanges();
                
            }

        }

        public void updateCity(City city)
        {
            using (LogisticDatabaseEntity ctx = new LogisticDatabaseEntity())
            {
                var c  = ctx.tblCity.FirstOrDefault(t => t.CityID == city.Id);
                if(c!=null)
                {
                    c.CityName = city.Name;
                    c.CityCountry = city.Country;

                    ctx.SaveChanges();
                }
            }
            return;
        }
        public void updateFactory(Factory factory)
        {
            return;
        }

    }
}
